package com.platform.controller;

import com.platform.cache.RedisTempUtil;
import com.platform.util.Constants;
import com.platform.util.KeyValue;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 临时动态字符串
 */
@Slf4j
@RestController
@RequestMapping("/temporary")
public class TemporaryController {
    @Autowired
    RedisTempUtil redisTempUtil;
    private static List<KeyValue> list = new ArrayList<>();


    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("temporary:list")
    public R list(@RequestParam Map<String, Object> params) {
        if (list.isEmpty()) {
            String share = (String) redisTempUtil.get(Constants.REDIS_SHARE_STR);
            list.add(KeyValue.builder().key(Constants.REDIS_SHARE_STR).value(share).build());
        }

        Query query = new Query(params);
        int total = list.size();
        PageUtils pageUtil = new PageUtils(this.list, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }


    /**
     * 查看信息
     */
    @RequestMapping("/save")
    @RequiresPermissions("temporary:save")
    public R save(@RequestBody KeyValue map) {
        redisTempUtil.set(map.getKey(), map.getValue());
        list.clear();
        return R.ok();
    }

    /**
     * 查看信息
     */
    @RequestMapping("/update")
    @RequiresPermissions("temporary:update")
    public R update(@RequestBody HashMap<String, String> map) {
        redisTempUtil.set(Constants.REDIS_SHARE_STR, map.get("value"));
        list.clear();
        return R.error("修改失败");
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info")
    @RequiresPermissions("temporary:info")
    public R info(@RequestParam("id") String id) {
        KeyValue temporary = null;
        for (KeyValue ll : list) {
            if (ll.getKey().equals(id)) {
                temporary = ll;
                break;
            }

        }

        return R.ok().put("temporary", temporary);
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("temporary:delete")
    public R delete(@RequestBody String key) {
        redisTempUtil.del(key);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/pass/delete")
    @RequiresPermissions("temporary:delete")
    public R passDelete(@RequestBody Integer[] ids) {


        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        // List<AuditEntity> list = auditService.queryList(params);

//        return R.ok().put("list", list);
        return null;
    }
}
